<?php
	class Answer_model extends MY_Model{
		
		public function __construct(){
			$this->load->database();
		}
		
		public function get_answer($id=FALSE, $q_id=FALSE){
			if($id === FALSE && $q_id===FALSE){
				$this->db->select('a.*, q.question, g.title');
				$this->db->from('answer a');
				$this->db->join('question q', 'q.id = a.question_id');
				$this->db->join('trivia_game g', 'g.id = q.trivia_game_id');
				$this->db->order_by('mark desc');
				$this->db->limit($this->record_per_page, $this->limit);
				$query = $this->db->get();
				return $query->result_array();
			}
			elseif($id===FALSE && $q_id!=FALSE){
				$this->db->select('a.*, q.question, g.title');
				$this->db->from('answer a');
				$this->db->join('question q', 'q.id = a.question_id');
				$this->db->join('trivia_game g', 'g.id = q.trivia_game_id');
				$this->db->where('a.question_id', $q_id);
				$this->db->order_by('mark desc');
				$this->db->limit($this->record_per_page, $this->limit);
				$query = $this->db->get();
				return $query->result_array();
			}
			else
				$query = $this->db->get_where('answer', array('answer.id' => $id));
			return $query->row_array();
		}
		
		public function get_answer_list(){
			$query = $this->db->get('answer');
			return $query->result_array();
		}
		
		public function get_total(){
			return count($this->get_answer_list());
		}
		
		public function get_total_filter($q_id){
			$this->db->select('a.*, q.question, g.title');
				$this->db->from('answer a');
				$this->db->join('question q', 'q.id = a.question_id');
				$this->db->join('trivia_game g', 'g.id = q.trivia_game_id');
				$this->db->where('a.question_id', $q_id);
				$this->db->order_by('mark desc');
				$query = $this->db->get();
				return $query->num_rows();
		}
		
		public function get_total_search($src=FALSE){
			$this->db->select('a.*, q.question, g.title');
			$this->db->from('answer a');
			$this->db->join('question q', 'q.id = a.question_id');
			$this->db->join('trivia_game g', 'g.id = q.trivia_game_id');
			$this->db->like('a.answer', $src);					
			$query = $this->db->get();				
			return $query->num_rows();
		}
		
		public function search($src=FALSE){
			$this->db->select('a.*, q.question, g.title');
			$this->db->from('answer a');
			$this->db->join('question q', 'q.id = a.question_id');
			$this->db->join('trivia_game g', 'g.id = q.trivia_game_id');
			$this->db->like('a.answer', $src);		
			$this->db->limit($this->record_per_page, $this->limit);
			$query = $this->db->get();				
			return $query->result_array();
		}
		
		public function save(){
			$id 		= $this->input->post('id');
			$q_id		= $this->input->post('question_id');
			
			$arr_anw= $this->input->post('answer');
			$arr_mark=$this->input->post('mark');
			
			$data = array();
			
				
			for($i=0; $i<count($arr_anw);$i++){
				$data = array(
					"answer" 		=> $arr_anw[$i],	
					"question_id"	=> $this->input->post('question_id'),
					"mark"			=> $arr_mark[$i]	
				);
				if(empty($id))
					$this->db->insert('answer', $data);
				else{
					$this->db->where('id', $id);
					$this->db->update('answer', $data);
				}
			}
			return TRUE;
		}
		
		public function delete($id){
			$this->db->where('id', $id);
			$this->db->delete('answer');
			
		}		
	}
?>